<script type="text/javascript">
$(document).ready(function(){
	getAllData();
	$("#reinForm").ajaxForm({
        dataType:  'json',
        success: showResponse
    });

    function showResponse(res) {
        if (res.error) {
            var message = '';
            $.each(res.error,function(i,val){
                $.each(val,function(ii,vv){
                    $("#"+i+"-"+ii).addClass('ui-state-error');
                    $.each(vv,function(iii,vvv){
                        message += $("#"+ii+"-label label").html()+': '+ vvv+ "\n";
                    });
                });
            });
            jAlert(message, '<?php echo $this->translate('checking errors')?>');
        }
        else{

            $("#rein-CancelBtn").trigger('click');
            getAllData();
//            $("#out-class_id").trigger('change');
        }
        $('#rein-data').unblock();
    }


    $("#rein-studentSn").autocomplete("<?php echo $this->route('rein-search') ?>", {
        width: 260,
        selectFirst: false,
    });

    $("#rein-studentSn").result(function(event, data, formatted) {
        if (data) {
            $("#rein-sn").val(data[1]);
            setReinData(data[1]);
            $("#reinForm").attr('action','<?php echo $this->route('rein-set')?>');
        }
    });

    $("#rein-SaveBtn").click(function(){
    	$(".ui-state-error").removeClass('ui-state-error ');
    	$('#rein-data').block({message:'<h1><?php echo $this->translate('Data setting ..')?></h1>',  border: '3px solid #f00'});
        $("#reinForm").submit();
    });

    $("#rein-CancelBtn").click(function(){
    	$(".ui-state-error").removeClass('ui-state-error ');
        $("#reinForm").attr('action','<?php echo $this->route('rein-set')?>');
        $("#reinForm input:text").val('').attr('disabled','');
        $("#reinForm input:hidden").val('');
        $("#rein-CancelBtn").attr('disabled','disabled');
    //    $("#out-class_id").attr('disabled','');
    //    $("#out-student_sn").attr('disabled','');
        $("#rein-edit-message").html('');
    });

    $("#rein-zoom-in").click(function(){
        $.openDOMWindow({
            loader:1,
            loaderImagePath:'animationProcessing.gif',
            loaderHeight:16,
            loaderWidth:17,
            width : 960,
            windowSourceID:'#rein-student-list'
        });
    });

    function getAllData(){
        $.get('<?php echo $this->route('get-student')?>',{kind:'rein'},function(data){
            $("#rein-student-list").html(data);
        });
    }
});


//取回復學資料
function setReinData(sn) {
   $.getJSON('<?php echo $this->route('get-transfer-data-byid')?>',{sn:sn},function(data){
      $.each(data,function(i,v){
          $("#rein-"+i).val(v);
      });
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth()+1;
      var curr_year = d.getFullYear();


      $("#rein-date").val(curr_year+'-'+curr_month+'-'+curr_date);
      $("#rein-studentSn").val(data.name);

      var class_sn = data.grade*100+data.class_sn*1;
      $("#rein-selClass").val(class_sn);

      $("#rein-SaveBtn").attr('disabled','');
      $("#rein-CancelBtn").attr('disabled','');
      $("#rein-edit-message").html(data.name+'  <?php echo $this->translate('editting..')?>');
  });
}
</script>
<style>
#rein-student-list {overflow: auto;height:350px;width:600px;background:#ffe;border:thin solid #ccc;padding:3px;}
#rein-student-list table {white-space: nowrap;}
</style>
<div id="rein-data">
<div id="rein-dataForm" style="float:left;width:250px">

<form action="<?php echo $this->route('rein-set')?>" id="reinForm" method="post" >
<input type="button" id="rein-SaveBtn" value="<?php echo $this->translate('save')?>" />
<input type="button" id="rein-CancelBtn" value="<?php echo $this->translate('cancel')?>"  disabled="true"/>
<span id="rein-edit-message" style="margin-left:5px; background:#ffa;padding:3px;border: solid thin #ccc"></span>
<div style="border:#ccc thin solid; padding:5px;margin:5px">
<?php echo $this->form->studentSn?>
<?php echo $this->form->causation_kind?>
<?php echo $this->form->selClass?>
<?php echo $this->form->student_id?>
<?php echo $this->form->number?>
<?php echo $this->form->date?>
<?php echo $this->form->approve_organ?>
<?php echo $this->form->approve_date?>
<?php echo $this->form->approve_word?>
<?php echo $this->form->approve_number?>
<?php echo $this->form->sn?>
</div>
</form>
</div>


<div id="rein-listTable" style="float:left; margin-left:10px">
<h2><span id="rein-year-semester-span">
<?php echo sprintf($this->translate('%d No. %d academic year semester'), $this->Semester['year'], $this->Semester['semester'])?></span>
 <?php echo $this->translate('all transfer rein data')?>
<img  id="rein-zoom-in" src="<?php echo $this->baseUrl?>/images/zoom-in.png"  style="margin-left:5px" />
</h2>
<div id="rein-student-list"></div>
</div>
<div style="clear:both"></div>
</div>
